package cn.terminus.biz.customer.contractinfo;

import cn.terminus.biz.customer.common.AbstractTerminusManager;
import cn.terminus.common.customer.bean.analysis.AnalysisYearDto;
import cn.terminus.common.customer.bean.analysis.ContractTypeRoomDetail;
import cn.terminus.common.customer.bean.contractinfo.dto.*;
import cn.terminus.common.customer.bean.custompurposeroom.dto.CustomPurposeRoomResultDto;
import cn.terminus.data.customer.contractinfo.dao.ContractInfoDao;
import cn.terminus.result.common.exception.DataAccessException;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import cn.terminus.result.common.util.BeanCopyUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 合同信息表(ContractInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-07-21 11:01:06
 */
@Component
public class ContractInfoManager extends AbstractTerminusManager {
    @Autowired
    private ContractInfoDao contractInfoDao;

    public Integer add(ContractInfoNewDto dto) throws DataAccessException {
        try {
            return contractInfoDao.add(dto);
        } catch (Exception e) {
            logger.error("ContractInfoManager新增失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer update(ContractInfoEditDto dto) throws DataAccessException {
        try {
            return contractInfoDao.update(dto);
        } catch (Exception e) {
            logger.error("ContractInfoManager更新失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<ContractInfoResultDto> gets(ContractInfoGetsDto dto) throws DataAccessException {
        try {
            return contractInfoDao.gets(dto);
        } catch (Exception e) {
            logger.error("ContractInfoManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<ContractInfoResultDto> query(ContractInfoQueryDto dto) throws DataAccessException {
        try {
            return contractInfoDao.query(dto);
        } catch (Exception e) {
            logger.error("ContractInfoManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer delete(ContractInfoDeleteDto dto) throws DataAccessException {
        try {
            return contractInfoDao.delete(dto);
        } catch (Exception e) {
            logger.error("ContractInfoManager删除失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public TerminusPageAble<ContractInfoResultDto> page(ContractPageQueryDto dto, TerminusPageInfo page) throws DataAccessException {
        try {
            String orderBy = "create_time" + " desc";
            PageMethod.startPage(page.getCurrentPage(), page.getPageSize(), orderBy);
            Page<ContractInfoResultDto> result = (Page<ContractInfoResultDto>) contractInfoDao.queryWithPage(dto);
            return new TerminusPageAble<>(BeanCopyUtils.copy(result.getResult(), ContractInfoResultDto.class), page, result.getTotal());
        } catch (Exception e) {
            logger.error("ContractInfoManager分页查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<ContractInfoResultDto> getByCustomerId(Long cboCustomerId) throws DataAccessException {
        try {
            return contractInfoDao.getByCustomerId(cboCustomerId);
        } catch (Exception e) {
            logger.error("ContractInfoManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer deleteByIds(List<Long> ids) throws DataAccessException {
        try {
            return contractInfoDao.deleteByIds(ids);
        } catch (Exception e) {
            logger.error("ContractInfoManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<CustomPurposeRoomResultDto> geturposeRoomInfoByContractIds(List<Long> contractIdList) throws DataAccessException {
        try {
            return contractInfoDao.geturposeRoomInfoByContractIds(contractIdList);
        } catch (Exception e) {
            logger.error("ContractInfoManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer getByCustomerIdList(List<Long> ids) throws DataAccessException {
        try {
            return contractInfoDao.getByCustomerIdList(ids);
        } catch (Exception e) {
            logger.error("ContractInfoManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer analysisTotalContract(AnalysisYearDto dto) throws DataAccessException{
        try {
            return contractInfoDao.analysisTotalContract(dto);
        } catch (Exception e) {
            logger.error("ContractInfoManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<ContractTypeRoomDetail> queryByIds(List<Long> ids)throws DataAccessException{

        try {
            return contractInfoDao.queryByIds(ids);
        } catch (Exception e) {
            logger.error("ContractInfoManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }
}
